package com.leo.train.business.controller.admin;

import com.leo.train.business.dto.TrainInfoQueryDto;
import com.leo.train.business.entity.TrainInfo;
import com.leo.train.business.service.TrainInfoService;
import com.leo.train.common.resp.CommonResp;
import com.leo.train.common.resp.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin/trainInfo")
public class AdminTrainInfoController {
    @Autowired
    private TrainInfoService trainInfoService;
    /**
     * 新增
     * @param trainInfo
     * @return
     */
    @PostMapping("add")
    public CommonResp save(@RequestBody TrainInfo trainInfo) {
        trainInfoService.save(trainInfo);
        return new CommonResp();
    }

    /**
     * 修改
     * @param trainInfo
     * @return
     */
    @PostMapping("/update")
    public CommonResp update(@RequestBody TrainInfo trainInfo) {
        trainInfoService.save(trainInfo);
        return new CommonResp();
    }

    /**
     * 删除
     * @param map
     * @return
     */
    @PostMapping("/delete")
    public CommonResp delete(@RequestBody Map<String,Long> map){
        trainInfoService.delete(map.get("id"));
        return new CommonResp<>();
    }

    /**
     * 查询火车列表
     * @return
     */
    @GetMapping("/list")
    public CommonResp<PageResult<TrainInfo>> list(TrainInfoQueryDto trainInfoQueryDto) {
        PageResult<TrainInfo> pageResult = trainInfoService.list(trainInfoQueryDto);
        return new CommonResp<>(pageResult);
    }

    /**
     * 查询火车详情信息
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public  CommonResp<TrainInfo> detail(@RequestParam Long id) {
        TrainInfo trainInfo = trainInfoService.detail(id);
        return new CommonResp<>(trainInfo);
    }

}
